<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class AdminController extends Controller{

	public function login(Request $request){
		$username = $request->input('username');
		$password = $request->input('password');

		$result_arr = array();

		if(empty($username) || empty($password)){
			$result_arr = array(
					'code'=>'2',
					'msg'=>'所有字段不能为空'
			);
		}else{
			$users = DB::table('admins')->where("username", "=", $username)->get() or [];
			if(count($users) == 0){
				$result_arr = array(
						'code'=>'1',
						'msg'=>'用户不存在',
						'result' => "{}"
				);
			}else{
				$user = $users[0];
				if($user->password != $password){
					$result_arr = array(
							'code'=>'2',
							'msg'=>'密码错误',
							'result' => "{}"
					);
				}else{
					$result_arr = array(
							'code'=>'0',
							'msg'=>'ok',
							'result'=>array(
									'username'=>$user->username,
									'realname'=>$user->realname,
									'company'=>$user->company,
									'sid'=>$user->sid
							)
					);
				}

			}
		}
		return response()->json($result_arr);
	}

}

?>